#Set the grid and background
stage.set_axis(100)
stage.create_grid_overlay(10, "grey")
stage.set_background("soccerfield")
#Create coordinates for ball, create ball
ballx = -90
bally = random.randint(-100, 100)
ball = codesters.Sprite("soccerball", ballx, bally)
#Create coordinates for net, create net
netx = 80
nety = random.randint(-100, 100)
net = codesters.Sprite("soccernet", 80,nety)
net.flip_right_left()
#Draw line between ball and net
line = codesters.Line(-90, bally, 80, nety, "blue")
slopeguess = float(line.ask("What is the slope of the line?"))
#Calculate slope
rise = nety-bally
run = netx-ballx
slope = rise/run
#Display slope
# sprite = codesters.Text("text", x, y)
sprite = codesters.Text(slope, 0, 30)
#Calculate new point for ball
newrise = (slopeguess*run)
endy = bally + newrise
ball.glide_to(netx, endy)
rounded_error = round(abs(slopeguess-slope), 3)
score = False
topnet = nety + 38
bottomnet = nety - 35
blanktext = " "
endtext = codesters.Text(blanktext, 0, 15)
text1 = "The slope is positive and so was your guess!"
text1a = "The slope is negative and so was your guess!"
text1b = "The slope is positive and your guess was negative!"
text1c = "The slope is negative and your guess was positive!"
text2 = "But you were off by " + str(rounded_error) + "!"
text3 = "Click Run to try again!"
if bottomnet <= endy <= topnet:
score = True
if score:
text = "Great job!"
endtext.set_text(text)
else:
text = "Hmmm..."
endtext = codesters.Text(text, 0, 15)
stage.wait(1)
if slope >=0 and slopeguess >=0:
endtext.set_text(blanktext)
endtext.set_text(text1)
stage.wait(1.5)
endtext.set_text(text2)
stage.wait(1.5)
endtext.set_text(text3)
if slope <0 and slopeguess <=0:
endtext.set_text(blanktext)
endtext.set_text(text1a)
stage.wait(1.5)
endtext.set_text(text2)
stage.wait(1.5)
endtext.set_text(text3)
if slope >= 0 and slopeguess <0:
endtext.set_text(blanktext)
endtext.set_text(text1b)
stage.wait(1.5)
endtext.set_text(text3)
if slope <=0 and slopeguess >0:
endtext.set_text(blanktext)
endtext.set_text(text1c)
stage.wait(1.5)
endtext.set_text(text3)
tester = TestManager()
tester.display_success_message("Great job! Click Run to try again, or Submit and Next to continue!")
-
Run Code
-
Activity Submitted!
Submit Work
-
Next Activity
-
Stop Running Code
-
Show Chart
-
Show Console
-
Reset Code Editor
-
Codesters How To (opens in a new tab)